<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\common\model\Area;
use app\common\model\Invoice as InvoiceModel;
use app\common\model\InvoiceLog as InvoiceLogModel;
use think\Db;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Invoice extends Home
{
    public $noNeedLogin = ['get_province','get_city','get_area'];

    //获取用户地址列表
    public function lists(){
        $userInfo = $this->userInfo;
        $data = InvoiceModel::field('id,company_name,tax_number,status')->where(['user_id'=>$userInfo['id']])->paginate();
        $this->result('查询成功',$data,200);
    }

    /**
     * 添加发票抬头
     */
    public function add(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $validate = new \app\api\validate\Invoice();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            //参数有默认抬头的时候修改其他默认的为2
            if($getData['status'] == 1){
                $invoiceData = InvoiceModel::where(['user_id'=>$userInfo['id'],'status'=>1])->select();
                if($invoiceData){
                    foreach ($invoiceData as $key=>$value){
                        InvoiceModel::where(['id'=>$value['id']])->update(['status'=>2]);
                    }
                }
            }
            $getData['user_id'] = $userInfo['id'];
            $insert = InvoiceModel::create($getData);
            if($insert){
                $this->result('添加成功','',200);
            }
            $this->result('添加失败，请重试','',204);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 删除发票抬头
     */
    public function del(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $del = InvoiceModel::where(['id'=>$getData['id'],'user_id'=>$userInfo['id']])->delete();
            if($del){
                $this->result('删除成功','',200);
            }
            $this->result('删除失败，请重试','',204);
        }
        $this->result('无效的请求','',204);
    }

    /**
     * 更新发票抬头
     */
    public function save(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $validate = new \app\api\validate\Invoice();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            //参数有默认抬头的时候修改其他默认的为2
            if($getData['status'] == 1){
                $invoiceData = InvoiceModel::where(['user_id'=>$userInfo['id'],'status'=>1])->select();
                if($invoiceData){
                    foreach ($invoiceData as $key=>$value){
                        InvoiceModel::where(['id'=>$value['id']])->update(['status'=>2]);
                    }
                }
            }
            $update = InvoiceModel::where(['id'=>$getData['id'],'user_id'=>$userInfo['id']])->update($getData);
            if($update){
                $this->result('更新成功','',200);
            }
            $this->result('更新失败，请重试','',204);
        }else{
            $getData = $this->request->get();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $userInfo = $this->userInfo;
            $data = InvoiceModel::where(['user_id'=>$userInfo['id'],'id'=>$getData['id']])->find();
            if(empty($data)){
                $this->result('查询成功','',200);
            }
            $this->result('查询成功',$data,200);

        }
        $this->result('无效的请求','',204);
    }


    /**
     * 设置为默认发票
     */
    public function def(){
        if($this->request->isPost()){
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            if(!isset($getData['id']) || empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $addressData = InvoiceModel::where('user_id',$userInfo['id'])->where('id','<>',$getData['id'])->select();
            if($addressData){
                foreach ($addressData as $key=>$value){
                    InvoiceModel::where(['id'=>$value['id']])->update(['status'=>2]);
                }
            }
            $update = InvoiceModel::where('user_id',$userInfo['id'])->where('id',$getData['id'])->update(['status'=>1]);
            if($update){
                $this->result('更新成功','',200);
            }
            $this->result('更新失败','',204);
        }

    }

    /**
     * 发票列表
     */
    public function open_invoice_list(){
        $userInfo = $this->userInfo;
        if($this->request->isGet()){
            $pageData = $this->request->get('limit');
            if(empty($pageData) || !isset($pageData)){
                $limit = 10;
            }else{
                $limit = $pageData;
            }
            $resData['invoice'] = $userInfo['invoice'];
            $resData['list'] = InvoiceLogModel::where(['user_id'=>$userInfo['id']])->field('id,company_name,total,created_at,status')->paginate($limit);
            $this->result('查询成功',$resData,200);
        }
    }

    /**
     * 开发票
     */
    public function open_invoice_add(){
        $userInfo = $this->userInfo;
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['id']) || !isset($getData['id'])){
                $this->result('缺少参数','',204);
            }
            if(empty($getData['money']) || !isset($getData['money'])){
                $this->result('缺少参数','',204);
            }
            if($getData['money'] >= $userInfo['invoice']){
                $this->result('开票金额不足','',204);
            }
//            $validate = new \app\api\validate\Invoice();
//            if (!$validate->scene('open')->check($getData)) {
//                $this->result($validate->getError(), '', 204);
//            }

            $invoiceRes = Db::name('qt_invoice')->where(['id'=>$getData['id'],'user_id'=>$userInfo['id']])->find();
            $insertData['user_id'] = $userInfo['id'];
            $insertData['company_name'] = $invoiceRes['company_name'];
            $insertData['company_address'] = $invoiceRes['province'].$invoiceRes['city'].$invoiceRes['county'].$invoiceRes['company_address'];
            $insertData['company_tel'] = $invoiceRes['company_tel'];
            $insertData['company_bank'] = $invoiceRes['company_bank'];
            $insertData['tax_number'] = $invoiceRes['tax_number'];
            $insertData['total'] = $getData['money'];
            $insertData['shou_name'] = $getData['shou_name'];
            $insertData['shou_phone'] = $getData['shou_phone'];
            $insertData['shou_address'] = $getData['shou_address'];
            $insertData['status'] = 1;
            $insertData['created_at'] = time();
            $user['invoice'] = $userInfo['invoice'] - $getData['money'];
            $user['use_invoice'] = $userInfo['use_invoice'] + $getData['money'];
            try{
                Db::startTrans();
                //修改订单状态
                Db::name('qt_invoice_log')->insert($insertData);
                Db::name('qt_users')->where('id',$userInfo['id'])->update($user);
                Db::commit();

            }catch (Exception $exception){
                Db::rollback();
                $this->result('开票失败','',204);
            }
            $this->result('开票成功','',200);
        }
    }

    /**
     * 获取默认发票
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDefault(){
        $userInfo = $this->userInfo;
        $data = Db::name('qt_invoice')->where(['user_id'=>$userInfo['id'],'status'=>1])->find();
        $this->result('查询成功',$data,200);
    }

    /**
     * 获取开票详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(){
        if($this->request->isGet()){
            $getData = $this->request->get();
            if(empty($getData['id']) || !isset($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $data = Db::name('qt_invoice_log')->where(['id'=>$getData['id']])->find();
            $this->result('查询成功',$data,200);
        }

    }


}
